// pages/information/hospital.js
const app = getApp()
let ServiceUrl = app.globalData.ServiceUrl;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hospital:[],//医院数据
    value:'', //关键词
    hid: '',
    display: 'none',
    latitude: '',
    longitude: ''
  },
  /**
   * 搜索框输入内容事件 
   */
  onChange(e) {
    console.log(e)
    let that = this
    that.hospital(e.detail)
    if(e.detail == null || e.detail == ''){
      that.setData({display:'none'})
    }else{
      that.setData({display:''})
    }
  },
  onSearch(e) {
    console.log(e)
    let that = this
    that.hospital(e.detail)
    if(e.detail == null || e.detail == ''){
      that.setData({display:'none'})
    }else{
      that.setData({display:''})
    }
  },
//数据回填方法
backfill: function (e) {
//  console.log(e)
  var id = e.currentTarget.id;
  for (var i = 0; i < this.data.hospital.length;i++){
    if(i == id){
      this.setData({
        value: this.data.hospital[i].hospital_name,
        display: 'none',
        hid: this.data.hospital[i].hid
      });
    }  
  }
  //回填数据后跳转到上一个页面
  wx.navigateBack({
    delta: 1,
  })
},
  /**
   * 医院查询 
   */
  hospital:function(e){
    let that = this;
    wx.getLocation({
      type: 'gcj02',
      success (res) {
//        console.log(res)
        if(res.errMsg=="getLocation:ok"){
//          console.log(res.longitude)
          wx.request({
            url: ServiceUrl + 'getHospital',
            data: {
              keyword: e,
              latitude: res.latitude,
              longitude: res.longitude
            },
            success(res){
              console.log(res)
              if(res.data.state == 200){
                that.setData({
                  hospital: res.data.hospital,
                })
              }else{
                wx.showToast({
                  title: res.data.msg,
                  icon: 'none'
                })
              }
            }
          })
          //console.log('获取定位成功')
        }else{
          console.log('获取定位失败')
        }
      }
     });
    
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    that.setData({typeid: options.typeid})
    that.getLocationInfo()
    console.log(that.data.typeid)
  },
  //获取经纬度并发送后台获取医院列表
  getLocationInfo(){
    let that = this
    wx.getLocation({
      type: 'gcj02',
      success (res) {
//        console.log(res)
        if(res.errMsg=="getLocation:ok"){
//          console.log(res.longitude)
          wx.request({
            url: ServiceUrl + 'getHospitalNear',
            data: {
              latitude: res.latitude,
              longitude: res.longitude
            },
            success(res){
              console.log(res)
              that.setData({
                hospital: res.data.hospital,
                display:''
              })
            }
          })
          //console.log('获取定位成功')
        }else{
          console.log('获取定位失败')
        }
      }
     });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    let that = this;
    //获取页面栈
    let pages = getCurrentPages();
    //获取所需页面
    let prevPage = pages[pages.length -2];//上一页
    if(that.data.value != ''){
      prevPage.setData({
        selectHospital: that.data.value,//你需要传过去的数据
        selectHospitalId: that.data.hid,
        typeid: that.data.typeid
      });
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    let that = this
    wx.stopPullDownRefresh({
      success: (res) => {
        that.getLocationInfo();
      },
    },3000)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})